﻿<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
        System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand();
        System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
        System.Data.DataSet ds = new System.Data.DataSet();        
        
        conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\demo.mdf;Integrated Security=True;User Instance=True";
        
        
        
        conn.Open();
                
        comm.CommandText = "GetMenu";              
        
        comm.Connection = conn;
        da.SelectCommand = comm;
        
        da.Fill(ds);
        foreach (System.Data.DataRow row in ds.Tables[0].Rows)
        {
            if (row["IdMenu"].Equals(row["IdMenuPadre"]))
            {
                MenuItem mnMenu = new MenuItem();
                mnMenu.Value = row["IdMenu"].ToString();
                mnMenu.Text = row["Nombre"].ToString();
                mnMenu.NavigateUrl = row["Link"].ToString();
                mMenu.Items.Add(mnMenu);
                AgregarMenuItem(mnMenu, ds.Tables[0]);
            }
        }
        
      
        ds.Dispose();
        da.Dispose();
        comm.Dispose();        
        conn.Close();         
    }
    
    private void AgregarMenuItem(MenuItem mnuMenuItem,System.Data.DataTable dtMenuItems)
    {
        foreach (System.Data.DataRow drMenuItem in dtMenuItems.Rows)
        {
            if (drMenuItem["IdMenuPadre"].ToString().Equals(mnuMenuItem.Value) && !drMenuItem["IdMenu"].Equals(drMenuItem["IdMenuPadre"]))
            {
                MenuItem mnMenu = new MenuItem();
                mnMenu.Value = drMenuItem["IdMenu"].ToString();
                mnMenu.Text = drMenuItem["Nombre"].ToString();
                mnMenu.NavigateUrl = drMenuItem["Link"].ToString();
                mnuMenuItem.ChildItems.Add(mnMenu);
                AgregarMenuItem(mnMenu, dtMenuItems);
            }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Página sin título</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<asp:LoginName ID="LoginName1" runat="server" />
        <asp:Menu ID="mMenu" runat="server" BackColor="#B5C7DE" DynamicHorizontalOffset="3"
            Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" StaticSubMenuIndent="10px" StaticDisplayLevels="3" Orientation="Horizontal" MaximumDynamicDisplayLevels="4">
            <StaticSelectedStyle BackColor="#507CD1" />
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <DynamicHoverStyle BackColor="#284E98" ForeColor="White" />
            <DynamicMenuStyle BackColor="#B5C7DE" />
            <DynamicSelectedStyle BackColor="#507CD1" />
            <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticHoverStyle BackColor="#284E98" ForeColor="White" />
        </asp:Menu>
   
    </div>
    </form>
</body>
</html>
